package com.qfhd.soft.cjzcore.UI.Custom;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qfhd.soft.cjzcore.CJZManager;
import com.qfhd.soft.cjzcore.Core.CJCore;
import com.qfhd.soft.cjzcore.Core.CJTime;
import com.qfhd.soft.cjzcore.Model.Task_image_item_model;
import com.qfhd.soft.cjzcore.Model.Task_try_model;
import com.qfhd.soft.cjzcore.Moudle.CJTryPlaceHelper;
import com.qfhd.soft.cjzcore.R;
import com.qfhd.soft.cjzcore.UI.Core.CJPackage;
import com.qfhd.soft.cjzcore.UI.Dialog.CJDialog;
import com.qfhd.soft.cjzcore.UI.Listener.CJListener;
import com.qfhd.soft.cjzcore.UI.Listener.CJOnlistListener;
import com.qfhd.soft.cjzcore.UI.MulitRecycle.adapter.MulitAdpter;
import com.qfhd.soft.cjzcore.UI.MulitRecycle.bean.Task_image_Bean;
import com.qfhd.soft.cjzcore.UI.MulitRecycle.bean.Task_try_Bean;
import com.qfhd.soft.cjzcore.UI.MulitRecycle.decorate.Visitable;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.util.ArrayList;
import java.util.List;

public class CJTryPlaceView extends RelativeLayout {
    private Context mContext;
    private Activity mActivity;
    private AttributeSet mAttr;
    private RecyclerView recyclerView;
    private MulitAdpter adpter;
    private QMUITipDialog waitDialog;
    private List<Visitable> dataSource;

    public CJTryPlaceView(Context context) {
        this(context,null);
    }

    public CJTryPlaceView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CJTryPlaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context,AttributeSet attr){
        mContext    = context;
        mAttr       = attr;

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mView              = inflater.inflate(R.layout.try_place_layout,this,true);
        recyclerView            = (RecyclerView) mView.findViewById(R.id.tryPlace_recycleV);

        adpter = new MulitAdpter();
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        recyclerView.getItemAnimator().setChangeDuration(0);
        recyclerView.setAdapter(adpter);

        waitDialog = CJDialog.getTipDialog(CJDialog.CJDialog_TIP_TYPE_LOADING,mContext,"获取中");
    }

    public List<Visitable> getDataSource(){
        return dataSource;
    }

    public boolean setData(List<Task_try_model> list, Context context, Activity activity){
        mActivity = activity;
        if(list.size() > 0) {
            dataSource = new ArrayList<>();
            for (Task_try_model model : CJTryPlaceHelper.getInstance().getTryTask_list()) {
                //获取试玩任务数据
                Task_try_Bean bean1 = new Task_try_Bean(mContext
                        , model
                        , object -> activity.runOnUiThread(() -> {
                    Task_try_Bean tmp2 = (Task_try_Bean) object;
                    getTask(tmp2);
                }));
                activity.runOnUiThread(() -> dataSource.add(bean1));
            }

            adpter.setList(dataSource);
            return true;
        }
        else{
            return false;
        }
    }

    private void getTask(Task_try_Bean tmp) {
        waitDialog.show();
        new Thread(() -> {
            boolean ret = CJZManager.getInstance().tryPlaceGetTask(tmp.getData().tid);
            if (ret) {
                tmp.getData().isGetTask = true;
                tmp.getData().timeStamp = (CJTime.getCurrentTimeMillis() + CJTime.getDifferenceTime()) / 1000;
                mActivity.runOnUiThread(()->{
                    adpter.setList(dataSource);
                });
            } else {
                CJCore.ToastInMainThread(mActivity,mContext,"获取失败",false);
            }
            waitDialog.dismiss();
        }).start();
    }

    public void setItemClickListener(CJOnlistListener listener){
        if(listener != null){
            adpter.setOnclickListener(listener);
        }
    }
}
